package com.css.dzj.dzjportal.modules.jgaction.apotherinfo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.dzj.dzjportal.common.util.R;
import com.css.dzj.dzjportal.common.util.excel.ExcelData;
import com.css.dzj.dzjportal.common.util.excel.ExportExcelUtils;
import com.css.dzj.dzjportal.common.util.http.HttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 安评其他行为
 */
@RestController
@RequestMapping("/other")
@PropertySource(value = "classpath:base.properties")
public class ApOtherController {
    @Autowired
    HttpServletRequest request;

    @Value("${AP_URL}")
    private String apurl;

    @GetMapping("/getPage")
    public R getPage(@RequestParam(required = false) String superviseItemCode,
                     @RequestParam(required = false) String recordUniqueIdentity,
                     @RequestParam(required = false) String actionName,
                     @RequestParam(required = false) String actionCode,
                     @RequestParam(required = false) String checkBehaviorNumber,
                     @RequestParam(required = false) String superviseObject,
                     @RequestParam(required = false) Integer pageNo, @RequestParam(required = false) Integer pageSize) {
        try {
            HashMap params = new HashMap<String, Object>();
            params.put("pageNo", pageNo.toString());
            params.put("pageSize", pageSize.toString());
            params.put("superviseItemCode", superviseItemCode);
            params.put("recordUniqueIdentity", recordUniqueIdentity);
            params.put("actionName", actionName);
            params.put("actionCode", actionCode);
            params.put("checkBehaviorNumber", checkBehaviorNumber);
            params.put("superviseObject", superviseObject);
            String result = HttpClientUtil.doGet(apurl + "/other/getPage", params);
            JSONObject data = JSON.parseObject(result);
            return R.ok().put("data", data);
        } catch (Exception e) {
            return R.error();
        }
    }

    /**
     * 根据参数查询，全部导出
     *
     * @return
     */
    @GetMapping("/export")
    public R export(HttpServletResponse response,
                    @RequestParam(required = false) String superviseItemCode,
                    @RequestParam(required = false) String recordUniqueIdentity,
                    @RequestParam(required = false) String actionName,
                    @RequestParam(required = false) String actionCode,
                    @RequestParam(required = false) String checkBehaviorNumber,
                    @RequestParam(required = false) String superviseObject) {
        try {
            HashMap params = new HashMap<String, Object>();
            params.put("superviseItemCode", superviseItemCode);
            params.put("recordUniqueIdentity", recordUniqueIdentity);
            params.put("actionName", actionName);
            params.put("actionCode", actionCode);
            params.put("checkBehaviorNumber", checkBehaviorNumber);
            params.put("superviseObject", superviseObject);
            String result = HttpClientUtil.doGet(apurl + "/other/getList", params);
            JSONArray jsonApOthers = JSON.parseArray(result);
            // 导出
            export(jsonApOthers, response);
            return R.ok();
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 选择ids导出数据
     *
     * @param response
     * @param ids
     * @return
     */
    @GetMapping("/exportByIds")
    public R exportMaterialToExcel(HttpServletResponse response, @RequestParam String ids) {
        try {
            HashMap params = new HashMap<String, Object>();
            params.put("ids", ids);
            String result = HttpClientUtil.doGet(apurl + "/other/getListByIds", params);
            JSONArray jsonApOthers = JSON.parseArray(result);
            // 导出
            export(jsonApOthers, response);
            return R.ok();
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 获取总数
     *
     * @return
     */
    @GetMapping("/getTotalCount")
    public R getTotalCount() {
        try{
            String a = HttpClientUtil.doGet(apurl + "/other/getTotalCount");
            int result = 0;
            if(!StringUtils.isEmpty(a)){
                result =  Integer.parseInt(JSON.parse(a).toString());
            }
            return R.ok().put("data", result);
        }catch (Exception e){
            return R.error(e.getMessage());
        }
    }

    /**
     * 导出
     *
     * @param jsonApOthers
     */
    public  void export(JSONArray jsonApOthers, HttpServletResponse response) {
        // 创建导出实体
        ExcelData data = new ExcelData();
        data.setName("安评其他行为表");
        /**
         * 设置Excel标题
         */
        ArrayList<String> titles = new ArrayList<>();
        titles.add("监管事项目录编码");
        titles.add("记录唯一标识");
        titles.add("行为名称");
        titles.add("行为编号");
        titles.add("检查行为编号");
        titles.add("监管对象");
        data.setTitles(titles);
        /**
         * 处理行数据
         */
        ArrayList<List<Object>> rows = new ArrayList<>();
        for (int i = 0; i < jsonApOthers.size(); i++) {
            ArrayList<Object> row = new ArrayList<>();
            Map<String, Object> eventMap = JSONObject.parseObject(jsonApOthers.get(i).toString());
            row.add(eventMap.get("superviseItemCode"));
            row.add(eventMap.get("recordUniqueIdentity"));
            row.add(eventMap.get("actionName"));
            row.add(eventMap.get("actionCode"));
            row.add(eventMap.get("checkBehaviorNumber"));
            row.add(eventMap.get("superviseObject"));
            rows.add(row);
        }
        data.setRows(rows);
        try {
            // 导出
            ExportExcelUtils.exportExcel(request,response, "安评其他行为表.xlsx", data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
